<?php namespace App\Http\Controllers\Site;
use App\Models\User;
use App\Http\Controllers\Controller;
use App\Models\Comment;
use App\Models\News;
use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class ArticleController extends Controller{
    public function getFixedincomelist($id=0)//  这是安心投的方法
    {
            $data=DB::table('financial')->whereIn('fproductTypeId',[2,6,7,8])->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'fixedincomelist' ,
            'data'=>$data,
            'appends'=> $id ]);
//        echo "安心投";
    }


    public function getFixedincomedetail($id=null)//  这是安心投的方法
    {
        $data=DB::table('financial')->find($id);
        $for=DB::table('financial')->whereIn('fproductTypeId',[2,6,7,8])->take(5)->get();
//       print_r($data);
//        exit();
        return view( 'site.article.fixedincomedetail',['data'=>$data,'id'=>$id,'for'=>$for]);
//        echo "安心投";
    }

    public function getProjectlist($id=0)//  这是项目贷的方法
    {
        $data=DB::table('financial')->whereIn('fproductTypeId',[3])->paginate(12);
        return view( 'site.article.fixedincomelist',['page'=>'fundlist','data'=>$data,'appends'=> $id]);
//        echo "项目贷";
    }
    public function getProjectdetail()// 这是项目贷详情
    {

        return view( 'site.article.funddetail');
//        echo "基金详情";
    }
    public function getBorrowlist($id=0)//  这是快速借的方法
    {
        $data=DB::table('financial')->whereIn('fproductTypeId',[4,10])->paginate(12);

        return view( 'site.article.fixedincomelist',['page'=>'borrowlist','data'=>$data,'appends'=> $id]);
//        echo "快速借";
    }
    public function getSilkbaglist($id=0)//  这是锦囊的方法
    {
        $user = Auth::user();
        $list = DB::table('news')->paginate(12);  #列表读的是锦囊新闻
        $lists =DB::table('news')->get();   #这是读取的是全部新闻
        $comment = DB::table('comment')->where('id',$id)->paginate(2);           #评论

//        News::where('id',$id)->increment('clicks');
        return view( 'site.article.silkbaglist',[
            'page'=>'silkbaglist',
            'user' => $user,
            'list' => $list,
            'lists' => $lists,
            'appends'=> $id,
            'comment' => $comment,
        ]);
//        echo "锦囊";
    }

    public function getSilkbagdetail($id=0)//  这是锦囊detail
    {
        $user = Auth::user();
        $detail = DB::table('news')->where('id',$id)->get();    #具体的内容
        $clicks = DB::table('news')->where('id',$id)->increment('clicks');       #阅读量
        $lists =DB::table('news')->get();                       #推荐文章列表
        $comment = DB::table('comment')->where('id',$id)->paginate(2);           #评论
        return view( 'site.article.silkbagdetail',[
            'user' => $user,
            'detail' =>$detail,
            'lists' => $lists,
            'comment' => $comment,
            'appends'=> $id,
            'clicks' => $clicks
        ]);
    }
    public function getNewslist($id=0) #获取最新新闻
    {
        $list = DB::table('news')->where('type','4')
            ->orderby('updated_at','desc')
            ->paginate(12);  #列表读的是锦囊新闻
        return view('site.article.newslist',[
            'list' => $list,
            'page'=>'newslist',
            'appends'=> $id,
        ]);
    }
    public function getNoticelist($id=0)
    {
        //最新公告列表页面
        $noctice = DB::table('news')->where('type','2')
            ->orderby('updated_at','desc')
            ->paginate(12);
        return view('site.article.noticelist',[
            'notice' => $noctice,
            'page'=>'noticelist',
            'appends'=> $id,
        ]);
    }
    public function getMedia($id=0)
    {
        //最新公告列表页面
        $media = DB::table('news')->where('type','5')
            ->orderby('updated_at','desc')
            ->paginate(12);
        return view('site.article.media',[
            'media' => $media,
            'page'=>'media',
            'appends'=> $id,
        ]);
    }
    public function getNews($id=0)
    {
        $detail = DB::table('news')->where('id',$id)->get();    #具体的内容
        $lists =DB::table('news')->get();                       #推荐文章列表
        return view('site.article.news',[
            'detail' =>$detail,
            'lists' =>$lists
        ]);
    }
    public function getAnswer($id=0)
    {
        $answer = DB::table('question')->where('id',$id)->get();
        return view('site.answer',[
            'answer' => $answer
        ]);
    }
    /*
     * yyl 点赞功能实现
     */
    public function postPrizeZan(Request $request)
    {
        $inputData = $request->all();
        //参数格式判断
        $rules = array(
            'newsId' => 'required|min:1',
            'userId' => 'required|min:1'
        );
        $validator = Validator::make( $inputData, $rules );
        if ($validator->fails())
        {
            $result['code'] = 40003;
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            die;
        }

        //判断用户对相应新闻是否已点赞
        $isZan = DB::table('user_behavior')->join('users', 'user_behavior.userId', '=', 'users.id')
                            ->join('news','user_behavior.newsId','=','news.id')
                            ->where('users.id', '=', $inputData['userId'])
                            ->where('news.id', $inputData['newsId'])
                            ->count();
        if( $isZan > 0 ) {
            $result['code'] = 40002;
            echo json_encode($result,JSON_UNESCAPED_UNICODE);
            die;
        }

        //获取新闻表中的点赞数prizes
        $prizes = DB::table('news')->select('prizes')->where('id', $inputData['newsId'])->first();

        //事务处理多个表语句操作
        DB::beginTransaction();

        //用户对文章点赞添加到用户行为表user_behavior
        $insert_ubehavior = DB::table('user_behavior')->insertGetId([
            'userId' => $inputData['userId'],
            'newsId' => $inputData['newsId']
        ]);

        //更新新闻表news中的点赞数
        $update_prizes = DB::table('news')->where('id', $inputData['newsId'])->update(array(
            'prizes' => $prizes->prizes + 1,
        ));

        $ok = strval($insert_ubehavior) != null && strval($update_prizes) != null;
        if( $ok ){
            DB::commit();
        }else{
            Log::info( 'update error' );
            DB::rollback();
        }
        $result['code'] = !$ok ? 50001 : 200;
        echo json_encode( $result, JSON_UNESCAPED_UNICODE );
    }
//    public function postCommetent(Request $request,$id=0)
//    {
        #添加评论(为了名称上一致，添加的也就是comment评论功能，并不是锦囊的添加方法)
    public function postSilkbagdetail(Request  $request)
    {
        $data=$request->all();
        $attributes = [
            'comment'=>'评论',
        ];
        $rules = [
            'comment'=>'required',
        ];
        $messages = [
            'required'=>':attribute不能为空',
        ];
        $validator = Validator::make($data, $rules, $messages,$attributes);
        if ($validator->fails()) {
            $request->flash();
            return back()->withErrors($validator);
        }
        $data = [
            'comment' =>$data['comment'],
        ];
        $user = Comment::findOrNew(0);
        $user->fill($data);
        $user->save();

        return redirect('article/silkbagdetail');

    }
    public function getSelling()//这是热销的方法
    {
        return view('site.article.selling',['page'=>'selling']);
//        echo "热销";
    }

    public function getMyinformation()
    {
        return view('site.wechatlist.myinformation',[
            'page'=>'myinformation'
        ]);
        //echo "个人信息";
    }

    public function getAlllist($id=0)//  这是0-3个月的方法
    {
        $data=DB::table('financial')->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'alllist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "3个月";
    }
            //这是按多少月选择的投资方法
    public function getThreelist($id=0)//  这是0-3个月的方法
    {
        $data=DB::table('financial')->where('period','<=' ,3)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'threelist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "3个月";
    }
    public function getSixlist($id=0)//  这是6个月的方法
    {
        $data=DB::table('financial')->where('period','>' ,3)->where('period','<=',6)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'sixlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "6个月";
    }
    public function getNinelist($id=0)//  这是9个月的方法
    {
        $data=DB::table('financial')->where('period','>' ,6)->where('period','<=',9)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'ninelist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "9个月";
    }
    public function getTwelvelist($id=0)//  这是12个月的方法
    {
        $data=DB::table('financial')->where('period','>' ,9)->where('period','<=',12)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'twelvelist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "12个月";
    }

    public function getFourteenlist($id=0)//  这是大于12个月的方法
    {
        $data=DB::table('financial')->where('period','>' ,12)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'fourteenlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "12个月";
    }


    //这是按多少年利化率选择的投资方法
    public function getSevenlist($id=0)//  这是7%以下年利化率的方法
    {
        $data=DB::table('financial')->where('annualRate','<=' ,7)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'sevenlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "7%以下年利化率";
    }

    public function getEightlist($id=0)//  这是8%年利化率的方法
    {
        $data=DB::table('financial')->where('annualRate','>' ,7)->where('annualRate','<=' ,8)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'eightlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "8%年利化率";
    }


    public function getNinthlist($id=0)//  这是9%年利化率的方法
    {
        $data=DB::table('financial')->where('annualRate','>' ,8)->where('annualRate','<=' ,9)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'ninthlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "9%年利化率";
    }


    public function getTenlist($id=0)//  这是10%年利化率的方法
    {
        $data=DB::table('financial')->where('annualRate','>' ,9)->where('annualRate','<=' ,10)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'tenlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "10%年利化率";
    }

    public function getElevenlist($id=0)//  这是10%年利化率的方法
    {
        $data=DB::table('financial')->where('annualRate','>' ,10)->paginate(12);
//            print_r($data);
//            die();
        return view( 'site.article.fixedincomelist',
            ['page'=>'elevenlist' ,
                'data'=>$data,
                'appends'=> $id ]);
//        echo "10%以上年利化率";
    }
}